Conditions | 1 |
Paths | 1 |
Total Lines | 124 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | import fse from 'fs-extra' |
||
58 | export function save(url, tplPath, json = null, text = '', type = '', previousSave = null, realType = 'draft', publishAll = false) { |
||
59 | var dateStart = new Date() |
||
60 | |||
61 | url = cleanSlug(url) |
||
62 | |||
63 | var p = new Promise((resolve) => { |
||
64 | var isRejectedDoc = false |
||
65 | if(type === 'reject'){ |
||
66 | isRejectedDoc = true |
||
67 | url = Hooks.instance.trigger('beforeReject', url) |
||
68 | type = 'draft' |
||
69 | realType = 'draft' |
||
70 | url = Hooks.instance.trigger('afterReject', url) |
||
71 | } |
||
72 | var tplUrl = FileParser.getFileDataFromUrl(url) |
||
73 | type = type || FileParser.getType(url) |
||
74 | var pathIso = dateIso(tplUrl, type) |
||
75 | if(typeof previousSave !== 'undefined' && previousSave !== null){ |
||
76 | pathIso.jsonPath = path.join(config.root, previousSave.jsonPath.replace(config.root, '')).replace(/-abe-d/, `-abe-${realType[0]}`) |
||
77 | pathIso.htmlPath = path.join(config.root, previousSave.htmlPath.replace(config.root, '')).replace(/-abe-d/, `-abe-${realType[0]}`) |
||
78 | } |
||
79 | |||
80 | if (tplPath.indexOf('.') > -1) { |
||
81 | tplPath = fileUtils.removeExtension(tplPath) |
||
82 | } |
||
83 | var tpl = tplPath.replace(config.root, '') |
||
84 | |||
85 | var fullTpl = path.join(config.root, config.templates.url, tpl) + '.' + config.files.templates.extension |
||
86 | |||
87 | if(typeof json === 'undefined' || json === null) { |
||
88 | json = FileParser.getJson(tplUrl.json.path) |
||
89 | } |
||
90 | |||
91 | var ext = { |
||
92 | template: tpl.replace(/^\/+/, ''), |
||
93 | link: tplUrl.publish.link, |
||
94 | complete: 0, |
||
95 | type: type |
||
96 | } |
||
97 | |||
98 | let meta = config.meta.name |
||
99 | json[meta] = extend(json[meta], ext) |
||
100 | var date = fileAttr.get(pathIso.jsonPath).d |
||
101 | |||
102 | if (publishAll) { |
||
103 | if(typeof json[meta].publish !== 'undefined' && json[meta].publish !== null) { |
||
104 | date = json[meta].publish.date |
||
105 | } |
||
106 | }else { |
||
107 | if(typeof date === 'undefined' || date === null || date === '') { |
||
108 | date = new Date() |
||
109 | }else { |
||
110 | date = new Date(date) |
||
111 | } |
||
112 | } |
||
113 | Util.addMetas(tpl, json, type, {}, date, realType) |
||
114 | |||
115 | if(typeof text === 'undefined' || text === null || text === '') { |
||
116 | text = getTemplate(fullTpl) |
||
117 | } |
||
118 | |||
119 | Util.getDataList(fileUtils.removeLast(tplUrl.publish.link), text, json) |
||
120 | .then(() => { |
||
121 | |||
122 | json = Hooks.instance.trigger('afterGetDataListOnSave', json) |
||
123 | for(var prop in json){ |
||
|
|||
124 | if(typeof json[prop] === 'object' && Array.isArray(json[prop]) && json[prop].length === 1){ |
||
125 | var valuesAreEmpty = true |
||
126 | json[prop].forEach(function (element) { |
||
127 | for(var p in element) { |
||
128 | if(element[p] !== ''){ |
||
129 | valuesAreEmpty = false |
||
130 | } |
||
131 | } |
||
132 | }) |
||
133 | if(valuesAreEmpty){ |
||
134 | delete json[prop] |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | var obj = { |
||
140 | publishAll:publishAll, |
||
141 | type:type, |
||
142 | template:{ |
||
143 | path: fullTpl |
||
144 | }, |
||
145 | html: { |
||
146 | path:pathIso.htmlPath |
||
147 | }, |
||
148 | json: { |
||
149 | content: json, |
||
150 | path: pathIso.jsonPath |
||
151 | } |
||
152 | } |
||
153 | |||
154 | obj = Hooks.instance.trigger('beforeSave', obj) |
||
155 | |||
156 | obj.json.content[meta].complete = checkRequired(text, obj.json.content) |
||
157 | |||
158 | var res = saveJsonAndHtml(tpl.replace(/^\/+/, ''), obj, text) |
||
159 | if (isRejectedDoc) { |
||
160 | res.reject = fileAttr.delete(url).replace(path.join(config.root, config.draft.url), '') |
||
161 | } |
||
162 | |||
163 | Hooks.instance.trigger('afterSave', obj) |
||
164 | |||
165 | FileParser.copySiteAssets() |
||
166 | |||
167 | if(typeof config.publishAll !== 'undefined' && config.publishAll !== null && config.publishAll === true) { |
||
168 | if(!publishAll && type === 'publish') { |
||
169 | abeProcess('publish-all', [`FILEPATH=${json.abe_meta.link}`]) |
||
170 | } |
||
171 | } |
||
172 | |||
173 | log.duration('save: ' + url.replace(config.root, '') + ' (' + type + ')', ((new Date().getTime() - dateStart.getTime()) / 1000)) |
||
174 | resolve(res) |
||
175 | }).catch(function(e) { |
||
176 | console.error('Save.js', e) |
||
177 | }) |
||
178 | }) |
||
179 | |||
180 | return p |
||
181 | } |
||
182 | |||
261 | } |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: